<?php
function get_cart_total($cart)
{

    if (!isset($totalPrice)) {
        $totalPrice = null;
    }

    foreach ($cart as $key => $item) {
        foreach ($item as $key => $value) {
            if ($key == "itemprice")
                $totalPrice += $item["itemqty"] * $value;
        }
    }

    return $totalPrice;
}

function get_cart_totalItems($cart)
{

    if (!isset($totalItems)) {
        $totalItems = null;
    }

    foreach ($cart as $key => $item) {
        foreach ($item as $key => $value) {
            if ($key == "itemqty") {
                $totalItems += $value;

            }
        }
    }

    return $totalItems;
}

function generate_checkout_cart($carts)
{

    $cart = "<tr><td></td><td>Pid</td><td>Product name</td><td>Price per unit</td><td>Qty</td><td>Price</td></tr>";
    foreach ($carts as $cart_item) {
        $cart .= "<tr id='#item'><td><a href='javascript: removeItem(" . $cart_item["id"] . ")'><i class='icon-remove'></i></a></td><td>" . $cart_item["id"] . "</td><td><a href='product-detail.php?id={$cart_item['id']}'>" . $cart_item["itemname"] . "</a></td><td>" . $cart_item["itemprice"] . " €</td><td><button onclick='dcrcartItem({$cart_item['id']})' class='btn_qty'> - </button>" . $cart_item["itemqty"] . "<button onclick='ncrcartItem({$cart_item['id']})' class='btn_qty'> + </button></td><td>" . $cart_item["itemprice"] * $cart_item["itemqty"] . " €</td></tr>";
    }

    return $cart;
}

function toPermalink($str) {
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
    $clean = strtolower(trim($clean, '-'));
    $clean = preg_replace("/[\/_|+ -]+/", '-', $clean);

    return $clean;
}

?>